package com.sgcc.fj.pmos.common.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.alibaba.android.arouter.core.LogisticsCenter;
import com.alibaba.android.arouter.facade.Postcard;
import com.blankj.utilcode.util.LogUtils;

import com.sgcc.fj.pmos.common.bean.EventCenter;
import com.sgcc.fj.pmos.common.dialog.LoadingDialogUtils;
import com.sgcc.pmos.nativesdk.R;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 父类Fragment
 *
 * @author
 */
public abstract class BaseFragment extends SimpleImmersionFragment {

    private static final String TAG = "Base";
    protected Context mContext = null;
    public View mRootView;
    public String space = "";
    private boolean mIsViewCreated;
    private boolean isFirstVisible = true;

    /**
     * EventBus 回调方法
     *
     * @param eventCenter
     */
    public void onEventCallBack(EventCenter eventCenter) {

    }

    /**
     * 设置Fragment布局界面
     *
     * @return
     */
    protected abstract int getContentViewLayoutID();

    /**
     * 是否注册EventBus
     *
     * @return
     */
    public boolean isBindEventBusHere() {
        return false;
    }

    /**
     * 懒加载用 (第一次用户可见)
     */
    public void onFirstUserVisible() {
        isFirstVisible = false;
    }

    /**
     * 用户可见
     */
    public void onUserVisible() {
    }

    /**
     * 当用户不可见
     */
    public void onUserInvisible() {
    }

    /**
     * 初始化布局以及监听事件
     */
    protected void initViews() {

    }

    /**
     * 初始化业务操作
     */
    protected void initData() {

    }


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mContext = activity;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 判断是否注册EventBus
        if (isBindEventBusHere()) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // 根View 赋值
        mRootView = view;

        space = getResources().getString(R.string.space);
        mIsViewCreated = true;
        initViews();
        initData();
        setUserVisibleHint(getUserVisibleHint());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // 设置Fragment布局
        if (getContentViewLayoutID() != 0) {
            return inflater.inflate(getContentViewLayoutID(), null);
        } else {
            return super.onCreateView(inflater, container, savedInstanceState);
        }
    }


    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (!mIsViewCreated) return;
        //当Fragment在屏幕上可见并且没有加载过数据时调用
        if (isVisibleToUser && isFirstVisible) {
            onFirstUserVisible();
        } else if (isVisibleToUser) {
            onUserVisible();    // 非首次的每次可见
        } else {
            onUserInvisible();  // 不可见
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // 如果注册 则反注册
        try {
            if (isBindEventBusHere()) {
                EventBus.getDefault().unregister(this);
            }

        } catch (Exception e) {
        }
    }

    @Override
    public void initImmersionBar() {

    }

    /**
     * EventBus回调
     *
     * @param eventCenter
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(EventCenter eventCenter) {
        if (null != eventCenter) {
            LogUtils.i(TAG, "onEventMainThread() : " + "界面：" + this.getClass().getSimpleName()
                    + "有EventBus数据回调 code = " + eventCenter.getEventCode());
            onEventCallBack(eventCenter);
        }
    }

    public void showLoading() {
        LoadingDialogUtils.showLoadingDialog(getActivity());
    }

    public void showLoading(String msg) {
        LoadingDialogUtils.showLoadingDialog(getActivity(), msg);
    }

    public void showSPLoading(String title, String content) {
        LoadingDialogUtils.showSpecialLoadingDialog(getActivity(), title, content);
    }

    public void dismissLoading() {
        LoadingDialogUtils.dismissLoadingDialog(getActivity());
    }

    public void startForActivityResult(Fragment fragment, Postcard postcard, int requestCode) {
        LogisticsCenter.completion(postcard);
        Intent intent = new Intent(fragment.getActivity(), postcard.getDestination());
        intent.putExtras(postcard.getExtras());
        fragment.startActivityForResult(intent, requestCode);

    }


}
